<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>设备维保记录</title>
</head>

<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="javascript:history.go(-1);"></a>
			</div>
			<div class="header-title">设备维保记录管理</div>
		</div>
	</div>
	<div class="new-form">
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" style="margin: auto;">
					<tr>
						<td style="width: 80px;">项目:</td>
						<td>
							<input style="width: 100%;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: '../projectitemlist.do?status=2',
							    onSelect: function(rec){
   									var url2='../equipments.do?projectitemid='+rec.id;
   									$('#equipment').combobox('reload', url2);
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 80px;">设备名:</td>
						<td>
							<input style="width: 100%;" id="equipment" name="equipment" class="easyui-combobox" editable="false"
								data-options="valueField:'id',textField:'name',panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td>是否开机：</td>
						<td>
							<input class="easyui-checkbox" name="isopen" id="isopen">
						</td>
					</tr>
					<tr id="tr1" style="display: none;">
						<td style="width: 80px;">开机时间：</td>
						<td>
							<input class="easyui-datetimebox" id="starttime" name="starttime" style="width: 100%;" editable="false" />
						</td>
					</tr>
					<tr id="tr2" style="display: none;">
						<td style="width: 80px;">开机时长（小时）：</td>
						<td>
							<input class="easyui-textbox" id="intervaltime" name="intervaltime" style="width: 80px;"
								data-options="validType:'number'" />
						</td>
					</tr>
					<tr>
						<td>是否维保：</td>
						<td>
							<input class="easyui-checkbox" name="isdaily" id="isdaily">
						</td>
					</tr>
					<tr id="tr3" style="display: none;">
						<td style="width: 80px;">维保时间：</td>
						<td>
							<input class="easyui-datetimebox" id="createtime" name="createtime" style="width:100%;"
								editable="false" />
						</td>
					</tr>
					<tr>
						<td>备注：</td>
						<td>
							<input class="easyui-textbox" id="remark" name="remark" style="width: 100%;" />
						</td>
					</tr>
					<tr>
						<td>上传附件：</td>
						<td>
							<input class="easyui-filebox" id="images" name="images" style="width: 100%;"
								data-options="prompt:'添加文件', buttonText:'选择文件',multiple:true,accept:''" />
						</td>
					</tr>
				</table>
			</form>
			<div style="text-align: right;" class="search-input" id="div2">
				<a href="#" class="d-btn btn-green" onclick="displayAtt()">查看附件</a>
			</div>
			<div style="text-align: center;" id="div1">
				<a href="#" class="d-btn btn-blue" onclick="savePro()">保存</a>
			</div>
		</div>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 98%; max-height: 80%; padding: 5px; text-align: center;"></div>
	</div>
</body>
<script th:inline="javascript">
	var currentPro = [[${equipmentMark}]];
	$(function(){
		
	})
	$('#starttime').datetimebox({
		width:$("#projectitem").width(),
		onSelect : function(date) {
			var time = $('#starttime').datetimebox('spinner')
					.spinner('getValue');
			$('#starttime').datetimebox(
					'setText',
					date.getFullYear() + '-'
							+ (date.getMonth() + 1) + '-'
							+ date.getDate() + ' ' + time);
			$('#starttime').datetimebox('hidePanel');
		}
	});
	$('#createtime').datetimebox({
		width:$("#projectitem").width(),
		onSelect : function(date) {
			var time = $('#createtime').datetimebox('spinner')
					.spinner('getValue');
			$('#createtime').datetimebox(
					'setText',
					date.getFullYear() + '-'
							+ (date.getMonth() + 1) + '-'
							+ date.getDate() + ' ' + time);
			$('#createtime').datetimebox('hidePanel');
		}
	});
	$('#isopen').checkbox({
		onChange : function(r) {
			if (r){
				document.getElementById("tr1").style.display = "";
				document.getElementById("tr2").style.display = "";
			}else{
				document.getElementById("tr1").style.display = "none";
				document.getElementById("tr2").style.display = "none";
			}
		}
	})
	$('#isdaily').checkbox({
		onChange : function(r) {
			if (r){
				document.getElementById("tr3").style.display = "";
			}else{
				document.getElementById("tr3").style.display = "none";
			}
		}
	})
	$(document).ready(function() {
		if(currentPro != null){
			$("#projectitem").combobox("setValue", currentPro.equipment.item);
			$("#projectitem").combobox("disable");
			$("#equipment").combobox("setValue", currentPro.equipment);
			$("#equipment").combobox("disable");
			$("#remark").textbox("setValue", currentPro.remark);
			if (currentPro.starttime != null){
				$("#isopen").checkbox('check');
				$("#isopen").checkbox("disable");
				$("#starttime").datetimebox("setValue", parsedate(currentPro.starttime));
				$("#starttime").datetimebox("disable");
				$("#intervaltime").textbox("setValue", currentPro.intervaltime);
				$("#intervaltime").textbox("disable");
			}
			if (currentPro.isdaily == 1){
				$("#isdaily").checkbox('check');
				$("#isdaily").checkbox("disable");
				$("#createtime").datetimebox("setValue", parsedate(currentPro.createtime));
				$("#createtime").datetimebox("disable");
			}
		}else{
			document.getElementById('div2').style.display = 'none';
			currentPro = {};
		}
	})
	function savePro() {
		var formData = new FormData();
		if (currentPro.id){
			formData.append("id", currentPro.id);
		}
		formData.append("equipmentid", $("#equipment").combobox('getValue'));
		formData.append("starttime", $("#starttime").datetimebox('getValue'));
		formData.append("intervaltime", $("#intervaltime").textbox('getValue'));
		formData.append("remark", $("#remark").textbox('getValue'));
		if (document.getElementById("_easyui_checkbox_5").checked){
			formData.append("isdaily", 1);
		}else{
			formData.append("isdaily", 2);
		}
		formData.append("createtime", $("#createtime").datetimebox('getValue'));
		var images = document.getElementById("filebox_file_id_1").files;
		for(var i = 0;i < images.length;i++){
			formData.append('images', images[i]);
		}
		$.ajax({
			type : 'POST',
			url : "../equipmentMarkSave.do",
			cache : false,
			data : formData,
			processData : false,
			contentType : false,
			success : function(data) {
				if (data.success){
					$.messager.alert('保存设备维保记录', '保存设备维保记录成功', 'success');
				}else{
					$.messager.alert('保存设备维保记录', '保存设备维保记录失败', 'error');
				}
				window.setInterval("window.location.href=document.referrer", 2000);
			},
			dataType : "json"
		});
	}
	function displayAtt() {
		var row = currentPro;
		var tb = "";
		$.each(row.attachments,function(i,val) {
			if(isImage(val.url)){
				tb+= '<img onclick="window.open(\'../'+val.url+ 
				'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
				'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
			}else{
				tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
				'\')"><span>'+val.name+'</span></a><br>';
			}
		});
		$('#displayAtt').empty().html(tb);
		$('#displayAtt').dialog({
			title : '附件',
			resizable : true,
			closed : false,
			cache : false,
			modal : true
		});
	}
	function attachmentFile(id,name) {
		$.get('../attachmentFile.do?id=' + id, function(data) {
			blob = new Blob([ data ])
			var a = document.createElement('a');
			a.download = name;
			a.href = window.URL.createObjectURL(blob)
			a.click()
		})
	}
	function remove(id) {
		$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
			if (data){
				$.ajax({
					type : 'POST',
					url : "../attachmentDelete.do?id=" + id,
					contentType : "application/json",
					success : function(data) {
						if (data.success){
							$("#att" + id).remove();
							$.messager.alert('删除附件', '删除附件成功', 'success');
						}else{
							$.messager.alert('删除附件', '删除附件失败', 'error');
						}
						window.setInterval("location.reload()", 2000);
					},
					dataType : "json"
				});
			}
		});
	}
	function isImage(url) {
		var index= url.lastIndexOf(".");
		var ext = url.substr(index+1);
		return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ,'ico'].
				indexOf(ext.toLowerCase()) !== -1;
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear();
		var month = date.getMonth() + 1; //月份是从0开始的
		if (month < 10){
			month = "0" + month;
		}
		var day = date.getDate();
		if (day < 10){
			day = "0" + day;
		}
		var hour = date.getHours();
		if (hour < 10){
			hour = "0" + hour;
		}
		var min = date.getMinutes();
		if (min < 10){
			min = "0" + min;
		}
		var sec = date.getSeconds();
		if (sec < 10){
			sec = "0" + sec;
		}
		var newTime = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
		return newTime;
	}
	function parsedate(value) {
		var date = new Date(value);
		var year = date.getFullYear();
		var month = date.getMonth() + 1; //月份+1   
		var day = date.getDate();
		var hour = date.getHours();
		var minutes = date.getMinutes();
		var second = date.getSeconds();
		return day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + second;
	}
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		end : {
			validator : function(value,param) {
				var startDate = $("#startdate").val();
				var startTmp = new Date(startDate.replace(/-/g, "/"));
				var endTmp = new Date(value.replace(/-/g, "/"));
				return startTmp <= endTmp;
			},
			message : '结束时间不能早于开始时间！'
		},
		remainmoney : {
			validator : function(value,param) {
				return parseFloat(value) <= parseFloat(remainmoney);
			},
			message : '不能超过剩余金额'
		}
	});
</script>
</html>